/**
 * Created by Administrator on 2017/3/29.
 */

var tool = require('../../common/utils/appUtils');
var amap = require('../../common/utils/amap');

module.exports = {
    getNewFilm: function (params, callback) {
        tool.baseDao('getNewFilm', 'select * from movie order by publishdate desc limit ?,? ', params, function (result) {
            callback(result);
        });
    },
    getHotFilm: function (parmas, callback) {
        tool.baseDao('getHotFilm', 'select * from movie  limit ?,?', parmas, function (result) {
            callback(result);
        })
    },
    getScoreFilm: function (params, callback) {
        tool.baseDao('getHotFilm', 'select * from movie group by movie_name order by score desc   limit ?,?', params, function (result) {
            callback(result);
        })
    },
    getHeaderType: function (params, callback) {
        tool.baseDao('getHeaderType', 'select * from movie_type limit 1,?  ', params, function (result) {
            callback(result);
        });
    },
    getHeaderArea: function (params, callback) {
        tool.baseDao('getHeaderArea', 'select * from movie_country limit 1,?', params, function (result) {
            callback(result);
        });
    },
    getHeaderTime: function (params, callback) {
        tool.baseDao('getHeaderArea', 'select * from movie_year order by cast(movie_year as UNSIGNED INTEGER) desc limit 0,?', params, function (result) {
            callback(result);
        });
    },
    // getPageData: function (params, callback) {
    //     tool.baseDao('getPageData', 'select * from movie order by publishdate limit 6 union all select * from movie order by score limit 6 union all select * from movie order by score limit 6', params, function (result) {
    //         callback(result);
    //     });
    // },
    getCinemaByMid: function (params, callback) {
        var mid=params[0];
        var lat=params[1];
        var longt=params[2];

        amap.getCityAdcodeByPosition(lat,longt,function(data){
            if(data.isSuccess){

                if (lat&&longt) {
                    // 使用视图
                    // var sql = 'SELECT	a.cinema_code FROM	(SELECT	ci.cinema_code,	ROUND(6378.138 * 2 * ASIN(SQRT(POW(SIN((? * PI() / 180 - ci.cinema_lat * PI() / 180) / 2),	2) + COS(? * PI() / 180) * COS(ci.cinema_lat * PI() / 180) * POW(	SIN((? * PI() / 180 - ci.cinema_long * PI() / 180) / 2	),2))	) * 1000) AS dis FROM		cinema AS ci	WHERE	1 = 1	ORDER BY	dis		LIMIT 0,10) AS a'
                    var sql = 'SELECT	a.* FROM	(SELECT	ci.*,	ROUND(6378.138 * 2 * ASIN(SQRT(POW(SIN((? * PI() / 180 - ci.cinema_lat * PI() / 180) / 2),	2) + COS(? * PI() / 180) * COS(ci.cinema_lat * PI() / 180) * POW(	SIN((? * PI() / 180 - ci.cinema_long * PI() / 180) / 2	),2))	) * 1000) AS dis FROM		cinema AS ci ,cinema_region cig	 where ci.cinema_regionid=cig.id and cig.citycode=? and  ci.cinema_code in (select cinema_code from weixin_package_detail where mid=?) 	ORDER BY	dis	) AS a limit 10'
                    // var sql = 'SELECT	a.cinema_code FROM	(SELECT	ci.cinema_code,	ROUND(6378.138 * 2 * ASIN(SQRT(POW(SIN((? * PI() / 180 - ci.cinema_lat * PI() / 180) / 2),	2) + COS(? * PI() / 180) * COS(ci.cinema_lat * PI() / 180) * POW(	SIN((? * PI() / 180 - ci.cinema_long * PI() / 180) / 2	),2))	) * 1000) AS dis FROM		cinema where cinema_code in (select cinema_code from weixin_package_detail where mid=?) AS ci	ORDER BY	dis	) AS a'
                    // var fsql='select * from cinema where cinema_code in ('+sql+') limit 10';
                    // tool.baseDao('getCinemaByMid', 'select * from cinema where cinema_code in (select cinema_code from weixin_package_detail where mid=?) and ', params, function (result) {
                    tool.baseDao('getCinemaByMid', sql, [lat,lat,longt,data.citycode,mid], function (result) {
                        callback(result);
                    });
                }else {
                    // 使用视图

                    tool.baseDao('getCinemaByMid', 'select * from cinema where cinema_code in (select cinema_code from weixin_package_detail where mid=?) ', mid, function (result) {
                        // tool.baseDao('getCinemaByMid', sql, [lat,lat,longt,mid], function (result) {
                        callback(result);
                    });
                }



            }else{
                tool.baseDao('getCinemaByMid', 'select * from cinema where cinema_code in (select cinema_code from weixin_package_detail where mid=?) ', mid, function (result) {
                    // tool.baseDao('getCinemaByMid', sql, [lat,lat,longt,mid], function (result) {
                    callback(result);
                });
            }
        })




    },
    getCinemaRecommByMid: function (params, callback) {
        // 使用视图
        tool.baseDao('getCinemaByMid', 'select * from weixin_package_detail where mid=?', params, function (result) {
            callback(result);
        });
    },
    getRecomCinema: function (params, callback) {
        tool.baseDao('getRecomCinema', 'select * from cinema order by score desc limit ?,? ', params, function (result) {
            callback(result);
        });
    },

    getMovieInfoByParam:function (params, callback) {
        var sql = 'SELECT movie.poster,movie.mid,movie.movie_name,movie.director,movie.brief,movie.cast,movie.score,movie.country,movie.category,date_format(movie.publishdate, "%Y") as publishdate,movie.movie_bigpic ' +
            ' from  movie '+
            ' where movie.cinema_code = ' + param.cinemaCode + param.movieName  + param.country + param.publishdate + ' and movie.isdeleted != 1 and movie.down_flag=1 ';
        if(param.category != 0){
            sql += ' LEFT JOIN movietypes on movie.mid = movietypes.movie_id '+
                ' LEFT JOIN movie_type on movietypes.type_id = movie_type.id '+
                ' '+param.category +' group by movie.movie_name '+param.sort+' limit ?,10';
        }else{
            sql += ' group by movie.mid '+param.sort+' limit ?,10';
        }

        tool.baseDao('getMovieInfoByParam',sql,function (result) {
            callback(result);
        })
    },

    getMovieLib:function (param, callback) {

        var sql = 'SELECT movietp.* from (SELECT movie.poster,movie.mid,movie.movie_name,movie.director,movie.brief,movie.cast,movie.score,movie.country,movie.category,date_format(movie.publishdate, "%Y") as publishdate,movie.movie_bigpic ' +
            ' from  movie '+
            ' where  movie.isdeleted != 1 and movie.down_flag=1 '  + param.movieName  + param.country + param.publishdate + '   ) '+
            ' as movietp ';

        if(param.category != 0){
            sql +=
                // ' LEFT JOIN movietypes on movietp.mid = movietypes.movie_id '+
                // ' LEFT JOIN movie_type on movietypes.type_id = movie_type.id '+
                ' '+param.category +' group by movietp.movie_name '+param.sort+' limit ?,10';
        }else{
            sql += ' group by movietp.mid '+param.sort+' limit ?,10';
        }

        tool.baseDao('getMovieLib',sql,param.page,function (result) {
            callback(result);
        })



    }
}

